/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.pay;

import com.lxk.event.oms.enums.OrderEnums;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.service.finance.api.WalletServiceApi;
import com.lxk.service.finance.enums.AccountTypeEnum;
import com.lxk.service.finance.request.UpdateAccountParam;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;

import java.util.List;

/**
 * 余额充值，用于直接付款，非线上业务
 *
 * @author heyi
 * 2024/9/21
 */
@LiteflowComponent(value = FlowPayOrderNodeConst.PRE_RECHARGE_YU)
@AllArgsConstructor
public class PreRechargeYuNode extends NodeComponent {
    private final WalletServiceApi walletServiceApi;

    @Override
    public void process() throws Exception {
        PayContext contextBean = getContextBean(PayContext.class);
        Long userId = contextBean.getUserId();
        List<OmsOrderVo> omsOrder = contextBean.getOmsOrder();
        omsOrder.forEach(item -> {
            UpdateAccountParam add = new UpdateAccountParam();
            add.setUserId(userId);
            add.setExtraInfo("系统增加");
            add.setOrderSn("");
            add.setAmount(item.getActPayPrice());
            add.setAccountType(AccountTypeEnum.MONEY.getCode());
            walletServiceApi.add(add);
        });
    }

    @Override
    public boolean isAccess() {
        PayContext contextBean = getContextBean(PayContext.class);
        String payType = contextBean.getPayType();
        boolean equals = OrderEnums.PayType.YU.getCode().equals(payType);
        return contextBean.getYuForcePay() && equals;
    }
}
